#ifndef _DEF_UTIL
#define _DEF_UTIL
#include <stdlib.h>
#include <stdio.h>
#include <utility>
#include "defTypes.h"

#define SAFE_DELETE(x) { if ((x)!=NULL) { delete (x); x=NULL; } }
#define SAFE_DELETE_ARRAY(x) { if ((x)!=NULL) { delete [] (x); x=NULL; } }

// error codes
typedef unsigned int	ErrCode;
const unsigned int		error = 0;
const unsigned int		success = !error;

void WriteConsole(const char *fmt, ...);
#define FATAL_ERROR(msg) { printf("%s:%s\n",__FUNCTION__,msg); getchar(); exit(-1); }
/*
#if defined(linux) || defined(__CYGWIN__)
#define ERROR(why, fmt) { printf("Error (%s:%s line %d) : %s (" fmt ")\n", __FILE__, __FUNCTION__, __LINE__, why); exit(-1); }
#define WARNING(why, fmt) { printf("Warning (%s:%s line %d) : %s (" fmt ")\n", __FILE__, __FUNCTION__, __LINE__, why); }
#else
*/
#define ERROR(why, fmt, ...) { printf("Error (%s:%s line %d) : %s (" fmt ")\n", __FILE__, __FUNCTION__, __LINE__, why,__VA_ARGS__ ); exit(-1); }
#define WARNING(why, fmt, ...) { printf("Warning (%s:%s line %d) : %s (" fmt ")\n", __FILE__, __FUNCTION__, __LINE__, why,__VA_ARGS__ ); }
//#endif

#define swap(a,b,c) { c=a; a=b; b=c; }
#define min(a,b) ( (a)<(b) ? (a):(b) )
#define max(a,b) ( (a)>(b) ? (a):(b) )

// color
struct	color { uchar	r,g,b,a; };
struct	rgb	{ uchar	r,g,b; };

const color		
	black={0x00,0x00,0x00,0xff},	cyan   ={0x00,0xff,0xff,0xff}, 
	red  ={0xff,0x00,0x00,0xff},	magenta={0xff,0x00,0xff,0xff}, 
	green={0x00,0xff,0x00,0xff},	yellow ={0xff,0xff,0xff,0xff}, 
	blue ={0x00,0x00,0xff,0xff},	white  ={0xff,0xff,0xff,0xff};


#endif
